package com.itheima.service.cargo.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.common.utils.UtilFuns;
import com.itheima.dao.cargo.ContractDao;
import com.itheima.dao.cargo.ContractProductDao;
import com.itheima.dao.cargo.ExtCproductDao;
import com.itheima.domain.cargo.*;
import com.itheima.service.cargo.ContractProductService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 购销合同下   货物   的业务层
 */
@Service
public class ContractProductServiceImpl implements ContractProductService {

    @Autowired
    private ContractProductDao contractProductDao;

    @Autowired
    private ContractDao contractDao;

    @Autowired
    private ExtCproductDao extCproductDao;

    //查询销售排行
    @Override
    public List findSellData(String currentUserCompanyId) {
        return contractProductDao.findSellData(currentUserCompanyId);
    }

    //查询厂家销售额
    @Override
    public List findFactoryData(String currentUserCompanyId) {
        return contractProductDao.findFactoryData(currentUserCompanyId);
    }

    /**
     * 批量上传货物
     * @param contractProducts
     */
    @Override
    public void saveAll(List<ContractProduct> contractProducts) {
        //遍历循环保存货物
        for (ContractProduct contractProduct : contractProducts) {
            //调用持久层保存货物
            save(contractProduct);
        }
    }

    /**
     * 分页查询
     *
     * @param example
     * @param page
     * @param size
     */
    @Override
    public PageInfo findAll(ContractProductExample example, int page, int size) {
        PageHelper.startPage(page,size);
        List<ContractProduct> contractProducts = contractProductDao.selectByExample(example);
        return new PageInfo(contractProducts);
    }
    //根据id查询
    @Override
    public ContractProduct findById(String id) {
        return contractProductDao.selectByPrimaryKey(id);
    }
    //保存
    @Override
    public void save(ContractProduct contractProduct) {
        //1.判断是否输入了单价和数量
        double amount = 0d;
        if(contractProduct.getCnumber() != null && contractProduct.getPrice() != null){
            amount = contractProduct.getCnumber()*contractProduct.getPrice();
        }
        //2.设置货物的总金额
        contractProduct.setAmount(amount);
        //3.设置货物的id
        contractProduct.setId(UtilFuns.generateId());
        //4.根据合同id查询购销合同
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
        //5.设置合同的总金额
        contract.setTotalAmount(contract.getTotalAmount()+amount);
        //6.设置合同的货物款数
        contract.setProNum(contract.getProNum()+1);
        //7.保存货物
        contractProductDao.insertSelective(contractProduct);
        //8.更新购销合同
        contractDao.updateByPrimaryKeySelective(contract);
    }
    //更新
    @Override
    public void update(ContractProduct contractProduct) {

        //使用货物对象中的id，查询数据库中的货物
        ContractProduct dbcontractProduct = contractProductDao.selectByPrimaryKey(contractProduct.getId());
        //取出数据库中原有的总金额
        Double oldAmount = dbcontractProduct.getAmount();
        //1.判断是否输入了单价和数量
        double amount = 0d;
        if(contractProduct.getCnumber()!= null && contractProduct.getPrice()!= null){
            amount = contractProduct.getCnumber()* contractProduct.getPrice();
        }
        //2.设置货物的总金额
        contractProduct.setAmount(amount);
        //3.根据合同id查询购销合同
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
        //4.设置合同的总金额，它是合同的总金额-原有货物的总金额+现有货物的总金额
        contract.setTotalAmount(contract.getTotalAmount()-oldAmount+amount);
        //5.更新货物
        contractProductDao.updateByPrimaryKeySelective(contractProduct);
        //6.更新购销合同
        contractDao.updateByPrimaryKeySelective(contract);
    }
    //删除
    @Override
    public void delete(String id) {

        //1.根据id查询出货物
        ContractProduct contractProduct = contractProductDao.selectByPrimaryKey(id);    
        //获取货物的总金额
        double totalAmount = contractProduct.getAmount();
        //2.创建附件的查询条件对象
        ExtCproductExample extCproductExample = new ExtCproductExample();
        //3.设置查询条件
        extCproductExample.createCriteria().andContractIdEqualTo(id);
        //4.查询出来所有附件
        List<ExtCproduct> extCproducts = extCproductDao.selectByExample(extCproductExample);
        //5.把货物下的所有附件都要删掉
        double extAmount = 0d;
        for (ExtCproduct extCproduct : extCproducts) {
            extAmount+=extCproduct.getAmount();
            //删除附件
            extCproductDao.deleteByPrimaryKey(extCproduct.getId());
        }
        //删除货物
        contractProductDao.deleteByPrimaryKey(id);
        //6.根据id查询购销合同
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
        //7.设置购销合同的总金额
        contract.setTotalAmount(contract.getTotalAmount()-extAmount-totalAmount);
        //8.设置货物数-1
        contract.setProNum(contract.getProNum()-1);
        //9.设置附件数减去集合的长度
        contract.setExtNum(contract.getExtNum()-extCproducts.size());
        //10.更新购销合同
        contractDao.updateByPrimaryKeySelective(contract);
    }
}
